package com.flowerplus.store.common.structure;

import android.support.v7.util.DiffUtil;

import com.flowerplus.store.Logger;

import java.util.List;


/**
 * Created by z on.
 * Des:adapter 刷新数据工具类  无需调用notify
 * Date: 2017/3/31
 */
public class DiffCallback<T> extends DiffUtil.Callback {
    private List<T> newDatas, oldDatas;

    public DiffCallback(List<T> newDatas, List<T> oldDatas) {
        this.newDatas = newDatas;
        this.oldDatas = oldDatas;
    }

    @Override
    public int getOldListSize() {
        return oldDatas == null ? 0 : oldDatas.size();
    }

    @Override
    public int getNewListSize() {
        return newDatas == null ? 0 : newDatas.size();
    }

    /**
     * 判断两个item是否相同  如果返回false areContentsTheSame 便会被调用
     *
     * @param oldItemPosition
     * @param newItemPosition
     * @return
     */
    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        boolean equals = newDatas.get(newItemPosition).equals(oldDatas.get(oldItemPosition));
        Logger.d("areItemsTheSame "+equals);
        return equals;
    }

    /**
     * 判断两个item的数据是否相同 不相同才会刷新
     *
     * @param oldItemPosition
     * @param newItemPosition
     * @return
     */
    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {

        boolean equals = newDatas.get(newItemPosition).equals(oldDatas.get(oldItemPosition));
        Logger.d("areContentsTheSame "+equals);
        return equals;
    }
}
